const responseMessage = require("../response/response_message")
const {
    getNewFriendNotifyApi,
    addFriendNotifyApi,
    updateNotifyStatusApi
} = require("../papi/notify_api")



/**
* 获取新朋友申请或加群申请 
*/
exports.get_new_friend_notify = async (ctx) => {
    try{
        const { to_id } = ctx.request.query;
        const newNotify =  await getNewFriendNotifyApi(to_id)
        if(newNotify){
            ctx.body = new responseMessage(201,"success",newNotify)
        }else{
            ctx.body = new responseMessage(201,"暂无新申请",{})
        }
    }catch(error){
        ctx.body = new responseMessage(500,error.message,{})
    }
}

/**
* 添加好友申请或加群申请记录 
*/
exports.add_friend_notify = async (ctx) => {
    try{
        const notify = ctx.request.body;
        const notifyResult = await addFriendNotifyApi(notify);
        if(notifyResult){
            ctx.body = new responseMessage(201,"success",notifyResult)
        }else{
            ctx.body = new responseMessage(201,"添加失败")
        }
    }catch(error){
        ctx.body = new responseMessage(500,error.message,{})
    }
}

/**
* 修改申请记录的状态(是否已同意) 
*/
exports.update_notify_status = async (ctx,next) => {
    try{
        const { _id } = ctx.request.body
        const {modifiedCount} = await updateNotifyStatusApi(_id);
        if(modifiedCount > 0){
            return ctx.body = new responseMessage(201,"success",{});
        }else{
            return ctx.body = new responseMessage(201,"修改失败",{});
        }
    }catch(error){
        return ctx.body = new responseMessage(500,error.message,{})
    }
}